using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using System.Collections;
using System.Collections.Generic;
using System.Web;

namespace OA.BLL
{
    public class AspNetCache
    {
        private IMemoryCache _cache;

        public AspNetCache(IMemoryCache memoryCache)
        {
            _cache = memoryCache;
        }

        public static readonly string Prefix = "CY.OA";
        private static string GetKey(string key)
        {
            return Prefix + "." + key;
        }

        public T GetInfoByKey<T>(string key)
        {
            var obj = _cache.Get(GetKey(key));
            if (obj != null&& obj is T)
            {
                return (T)obj ;                 
            }
            else
            {
                return default(T);
            }
        }

        public void Insert<T>(T t,string key)
        {
            _cache.Set(GetKey(key), t);
        }

        public void Clear(string key)
        {
            _cache.Remove(GetKey(key));
        } 

    }
}